<template>
  <div v-loading="loading" class="container">
    <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="0" class="demo-ruleForm">

      <p>用户名</p>
      <el-form-item prop="userName">
        <el-input v-model="formData.userName" placeholder="请输入用户名" size="medium" clearable />
      </el-form-item>
      <p>密码</p>
      <el-form-item prop="password1">
        <el-input v-model="formData.password1" type="password" placeholder="请输入密码" size="medium" clearable />
      </el-form-item>
      <p>确认密码</p>
      <el-form-item prop="password2">
        <el-input v-model="formData.password2" type="password" placeholder="请输入密码" size="medium" clearable />
      </el-form-item>
      <p>姓名</p>
      <el-form-item prop="realName">
        <el-input v-model="formData.realName" placeholder="请输入姓名" size="medium" clearable />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" size="medium" :loading="submitLoading" @click="addAdmin()">{{ $route.query.adminId ? '编辑' :'创建' }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { uploadFile } from '@/api/admin.js'
import { addAdmin, getInfoById } from '@/api/web/admin'
import md5 from 'js-md5'
export default {
  data() {
    return {
      submitLoading: false,
      loading: false,
      formData: {},
      img_loading: false,
      bigLabels: [],
      smallLabels: [],
      smallList: [],
      rules: {
        userName: [{
          required: true,
          message: '请输入用户名'
        }],
        password1: [{
          required: true,
          message: '请输入密码'
        }],
        realName: [{
          required: true,
          message: '请输入姓名'
        }],
        roleSign: [{
          required: true,
          message: '请选择角色'
        }],
        password2: [{
          required: true,
          message: '请确认密码'
        }]
      }
    }
  },
  mounted() {
    if (this.$route.query.adminId) {
      this.formData = this.$route.query
    }
  },
  methods: {
    remoteMethod() {

    },
    getInfoById() {
      this.loading = true
      getInfoById({ adminId: +this.$route.query.adminId }).then(res => {
        this.loading = false
        if (res.code == 200) {
          this.formData = res.data
        } else {
          this.$message({
            type: 'error',
            message: res.msg
          })
        }
      })
    },
    addAdmin() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          delete this.formData.createDate
          this.formData.password = md5(this.formData.password1)
          addAdmin(this.formData).then(res => {
            this.submitLoading = false
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '提交成功'
              })
              this.$router.push({
                path: '/web/admin'
              })
            } else {
              this.$message({
                type: 'error',
                message: res.message
              })
            }
          })
        }
      })
    },
    uploadRequest(params) {
      this.img_loading = true
      uploadFile(params).then(res => {
        this.img_loading = false
        if (res && res.data && res.data.length) {
          this.$set(this.formData, 'cover', res.data[0])
        }
      })
    }
  }
}
</script>

<style scoped>
.avatar-uploader,
.el-upload {
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px !important;
  height: 178px;
}

.avatar-uploader:hover,
.el-upload:hover {
  border-color: #409EFF;
}

.upload-text {
  font-size: 14px;
  color: #8c939d;
  line-height: 178px;
  text-align: center;
  margin: 0 auto;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.container {
  padding-bottom: 30px;
}
</style>
<style>
.demo-ruleForm .el-input,
.demo-ruleForm .el-select {
  width: 300px;
}
</style>
